<?php
namespace app\shop\controller;
use app\AdminRest;
use longbingcore\wxcore\WxPay;
use think\App;
use think\facade\Db;
use think\Request;
use app\shop\model\AdminSellingCash as SellingCashModel;
use app\shop\model\AdminSellingProfit;
use app\shop\model\IndexUser;

class AdminSellingCash extends AdminRest
{

    protected $model;
    protected $profit_model;
    protected $user_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model        = new SellingCashModel();
        $this->profit_model = new AdminSellingProfit();
        $this->user_model   = new IndexUser();
    }
    /**
     * User: chenniang
     * Date: 2019-09-09 14:57
     * @return \think\Response
     * descption:提现列表
     */
    public function cashList(){
        $input = $this->_param;
        $dis[] = ['a.uniacid','=',$this->_uniacid];
        if(!empty($input['cash_no'])){
            $dis[] = ['a.cash_no','like',"%".$input['cash_no']."%"];
        }
        $data = $this->model->cashList($dis,$input['limit']);
        if(!empty($data['data'])){
            foreach ($data['data'] as &$v){
                $v['name'] = !empty($v['name'])?$v['name']:$v['nickName'];
            }
        }
        return $this->success($data);
    }
    /**
     * User: chenniang
     * Date: 2019-09-09 15:11
     * @return void
     * descption:确认到账
     */
    public function onCash(){

        $input = $this->_input;
        $data  = $this->model->cashInfo(['id'=>$input['id']]);
        if(empty($data)){
            return $this->error('no data');
        }
        if($data['status']!=0){
            return $this->error('status is error');
        }
        $profit = $this->profit_model->profitInfo(['user_id' => $data['user_id']]);
        if(empty($profit)){
            return $this->error('no data');
        }
        Db::startTrans();
        $res = $this->model->cashUpdate(['id'=>$data['id']],['status'=>1]);
        if($res){
            if ($profit['postaling']) {
                $postaling    = sprintf("%.2f", $profit['postaling'] - $data['money']);
                $total_postal = sprintf("%.2f", $profit['total_postal'] + $data['money']);
                $postaling    = $postaling<0?0:$postaling;
                $this->profit_model->profitUpdate(['user_id' => $data['user_id']],['postaling' => $postaling, 'total_postal' => $total_postal]);
            }
            $user = $this->user_model->userInfos(['id'=>$data['user_id']]);
            $send_data['open_id']   = $user['openid'];
            $send_data['uniacid']   = $this->_uniacid;
            $send_data['form_id']   = $this->getFormId($data['user_id']);
            $send_data['nickName']  = $user['nickName'];
            $send_data['send_body'] = '您提现的佣金已到账, 金额为:' . $data['money'] . ', 请注意查收';
            $send_data['page_data'] = 'longbing_card/voucher/pages/user/myearning/myearning';
            longbingSendWxServiceNoticeBase($send_data);
        }
        Db::commit();
        return $this->success($res);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-24 14:59
     * @功能说明:微信转账
     */
    public function wxCash(){




        $input = $this->_input;
        $data  = $this->model->cashInfo(['id'=>$input['id']]);
        if(empty($data)){
            return $this->error('no data');
        }
        if($data['status']!=0){
            return $this->error('status is error');
        }
        $profit = $this->profit_model->profitInfo(['user_id' => $data['user_id']]);
        if(empty($profit)){
            return $this->error('no data');
        }
        //获取用户openid
        $openid = Db::name('longbing_card_user')->where(['id'=>$data['user_id']])->value('openid');

        if(empty($openid)){

            return $this->error('用户信息错误，未获取到openid');
        }

        $data['money'] = round($data['money'],2);
        //微信相关模型
        $wx_pay = new WxPay($this->_uniacid);

        //微信提现
        $res    = $wx_pay->crteateMchPay($openid,$data['money']);



        if($res['result_code']=='SUCCESS'&&$res['return_code']=='SUCCESS'){

            $res = $this->model->cashUpdate(['id'=>$data['id']],['status'=>1]);
            if($res){
                if ($profit['postaling']) {
                    $postaling    = sprintf("%.2f", $profit['postaling'] - $data['money']);
                    $total_postal = sprintf("%.2f", $profit['total_postal'] + $data['money']);
                    $postaling    = $postaling<0?0:$postaling;
                    $this->profit_model->profitUpdate(['user_id' => $data['user_id']],['postaling' => $postaling, 'total_postal' => $total_postal]);
                }
                $user = $this->user_model->userInfos(['id'=>$data['user_id']]);
                $send_data['open_id']   = $user['openid'];
                $send_data['uniacid']   = $this->_uniacid;
                $send_data['form_id']   = $this->getFormId($data['user_id']);
                $send_data['nickName']  = $user['nickName'];
                $send_data['send_body'] = '您提现的佣金已到账, 金额为:' . $data['money'] . ', 请注意查收';
                $send_data['page_data'] = 'longbing_card/voucher/pages/user/myearning/myearning';
                longbingSendWxServiceNoticeBase($send_data);
            }

            return $this->success($res);

        }else{

            return $this->error(!empty($res['err_code_des'])?$res['err_code_des']:'你还未该权限');

        }

    }


}
